package cn.com.backstage.controller.base;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiU_advert_position;

import cn.com.api.service.ApiU_advert_positionService;

import cn.com.util.*;

public class U_advert_positionBaseController {

	private static Logger logger = Logger.getLogger(U_advert_positionBaseController.class);

	@Autowired
	private ApiU_advert_positionService apiU_advert_positionService;

	/**
	 * 列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "u_advert_position-list")
	public ModelAndView u_advert_position_list(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/u_advert_position_list.jsp");
		List<ApiU_advert_position> u_advert_positions = apiU_advert_positionService.findU_advert_positions();
		mv.addObject("u_advert_positions", u_advert_positions);
		return mv;
	}

	/**
	 * 进入新增或者编辑页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "u_advert_position-add-pre")
	public ModelAndView u_advert_position_add_pre(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/u_advert_position_add.jsp");
		//
		if (request.getParameter("id") != null) {
			String id = request.getParameter("id");
			ApiU_advert_position u_advert_position = apiU_advert_positionService
					.findU_advert_positionById(Integer.valueOf(id));
			mv.addObject("u_advert_position", u_advert_position);
		}
		return mv;
	}

	/**
	 * 新增或者编辑
	 * 
	 * @return
	 */
	@RequestMapping(value = "u_advert_position-add")
	public void u_advert_position_add(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		String position_name = request.getParameter("position_name");
		String pre_picture = request.getParameter("pre_picture");
		String unit_price = request.getParameter("unit_price");
		String unit = request.getParameter("unit");
		String spec = request.getParameter("spec");
		String ext1 = request.getParameter("ext1");
		String ext2 = request.getParameter("ext2");
		String ext3 = request.getParameter("ext3");
		String ext4 = request.getParameter("ext4");
		String ext5 = request.getParameter("ext5");
		String pre_prcture_inn = request.getParameter("pre_prcture_inn");
		String spec_inn = request.getParameter("spec_inn");

		Map<String, Object> map = new HashMap<String, Object>();
		// String id = request.getParameter("id");
		// add
		if (id == null || (id.trim()).equals("")) {
			ApiU_advert_position u_advert_position = new ApiU_advert_position();
			/*
			 * u_advert_position.setId(id);
			 * u_advert_position.setPosition_name(position_name);
			 * u_advert_position.setPre_picture(pre_picture);
			 * u_advert_position.setUnit_price(unit_price);
			 * u_advert_position.setUnit(unit); u_advert_position.setSpec(spec);
			 * u_advert_position.setExt1(ext1); u_advert_position.setExt2(ext2);
			 * u_advert_position.setExt3(ext3); u_advert_position.setExt4(ext4);
			 * u_advert_position.setExt5(ext5);
			 * u_advert_position.setPre_prcture_inn(pre_prcture_inn);
			 * u_advert_position.setSpec_inn(spec_inn);
			 */
			apiU_advert_positionService.insertU_advert_position(u_advert_position);
		}
		// edit
		else {
			ApiU_advert_position u_advert_position = apiU_advert_positionService
					.findU_advert_positionById(Integer.valueOf(id));
			/*
			 * u_advert_position.setId(id);
			 * u_advert_position.setPosition_name(position_name);
			 * u_advert_position.setPre_picture(pre_picture);
			 * u_advert_position.setUnit_price(unit_price);
			 * u_advert_position.setUnit(unit); u_advert_position.setSpec(spec);
			 * u_advert_position.setExt1(ext1); u_advert_position.setExt2(ext2);
			 * u_advert_position.setExt3(ext3); u_advert_position.setExt4(ext4);
			 * u_advert_position.setExt5(ext5);
			 * u_advert_position.setPre_prcture_inn(pre_prcture_inn);
			 * u_advert_position.setSpec_inn(spec_inn);
			 */
			apiU_advert_positionService.updateU_advert_position(u_advert_position);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	@RequestMapping(value = "u_advert_position-delete")
	public void u_advert_position_delete(HttpServletRequest request, HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");

		// delete
		int ret_i = apiU_advert_positionService.deleteU_advert_position(Integer.valueOf(id));

		//
		if (ret_i > 0) {
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

}
